package ch.zbw.pra.sysventory.application.controller;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;

import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.zbw.pra.sysventory.dataaccess.dataAccessObject.ScanversionDataAccessObject;
import ch.zbw.pra.sysventory.utils.DateUtils;

/**
 * Controller für das Statistik Panel
 * 
 * @author Ralph Koster
 */

public class StatisticsPanelController implements Serializable {

	private static final long serialVersionUID = -4848818841673367077L;
	private static final Logger LOG = LoggerFactory.getLogger(StatisticsPanelController.class);
	
	private ScanversionDataAccessObject scanversionDataAccessObject;
	
	public StatisticsPanelController() {
		this.scanversionDataAccessObject = new ScanversionDataAccessObject();
	}

	public String getNumberOfScansPerYear() {
		HashMap<String, LocalDate> yearRange = DateUtils.getCurrentYearRange();
		Integer numberOfScans = scanversionDataAccessObject.getNumberOfScansInTimerange(yearRange);
		return numberOfScans.toString();
	}
	
	public String getNumberOfScansPerMonth() {
		HashMap<String, LocalDate> monthRange = DateUtils.getCurrentMonthRange();
		Integer numberOfScans = scanversionDataAccessObject.getNumberOfScansInTimerange(monthRange);
		return numberOfScans.toString();
	}

	public String getNumberOfScansPerWeek() {
		HashMap<String, LocalDate> weekRange = DateUtils.getCurrentWeekRange();
		Integer numberOfScans = scanversionDataAccessObject.getNumberOfScansInTimerange(weekRange);
		return numberOfScans.toString();
	}
	
	public String getNumberOfScansInTimerange(Date startDate, Date endDate) {
		LocalDate dateFrom = DateUtils.convertDateToLocalDate(startDate);
		LocalDate dateTo = DateUtils.convertDateToLocalDate(endDate).plusDays(1);
		LOG.debug("Start date: " + dateFrom);
		LOG.debug("End date: " + dateTo);
		HashMap<String, LocalDate> timerange = new HashMap<String, LocalDate>();
		timerange.put("dateFrom", dateFrom);
		timerange.put("dateTo", dateTo);
		Integer numberOfScans = scanversionDataAccessObject.getNumberOfScansInTimerange(timerange);
		return numberOfScans.toString();
	}
}
